﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LibEBML.Common
{
    public class Segment : MatroskaBase
    {
        public Segment(EBMLContainer container)
            : base(container)
        {
            SeekPosition = container.DataPosition;
        }
        protected override void Initialize()
        {
            var m = container[0x1549a966].FirstOrDefault() as EBMLContainer;
            if (m != null)
            {
                Info = new SegmentInfo(m);
            }
            var ts = container[0x1654ae6b].FirstOrDefault() as EBMLContainer;
            if (ts != null)
            {
                Tracks = ts.Select(mt => new TrackEntry(mt as EBMLContainer)).ToList();
            }
            var cs = container[0x1c53bb6b].FirstOrDefault() as EBMLContainer;
            if (cs != null)
            {
                Cues = cs[0xbb].Select(mt => new CuePoint(mt as EBMLContainer)).ToList();
            }
            Clusters = container[0x1f43b675].Select(mt => new Cluster(mt as EBMLContainer)).ToList();
        }
        protected override void InitDefaultValues()
        {

        }

        public SegmentInfo Info { get; set; }
        public IList<TrackEntry> Tracks { get; set; }
        public IList<Cluster> Clusters { get; set; }
        public IList<SeekHead> SeekHead { get; set; }
        public IList<CuePoint> Cues { get; set; }
        public IList<Attachment> Attachments { get; set; }
        public IList<EditionEntry> Chapters { get; set; }
        public IList<Tag> Tags { get; set; }
        public long SeekPosition { get; private set; }
    }
    public class Tag : MatroskaBase
    {
        public Tag(EBMLContainer container)
            : base(container)
        {

        }
        protected override void Initialize()
        {

        }
        protected override void InitDefaultValues()
        {
        }

        public IList<Target> Targets { get; set; }
        public IList<SimpleTag> SimpleTags { get; set; }
    }
    public class Target : MatroskaBase
    {
        public Target(EBMLContainer container)
            : base(container)
        {

        }
        protected override void Initialize()
        {

        }
        protected override void InitDefaultValues()
        {
        }
    }
    public class SimpleTag : MatroskaBase
    {
        public SimpleTag(EBMLContainer container)
            : base(container)
        {

        }
        protected override void Initialize()
        {

        }
        protected override void InitDefaultValues()
        {
        }
    }
    public class SegmentInfo : MatroskaBase
    {
        public SegmentInfo(EBMLContainer container)
            : base(container)
        {

        }
        protected override void Initialize()
        {
            var m = container[0x7ba9].FirstOrDefault() as EBMLString;
            if (m != null)
            {
                Title = m.Value;
            }
            var m2 = container[0x2ad7b1].FirstOrDefault() as EBMLType<ulong>;
            if (m2 != null)
            {
                TimecodeScale = (long)m2.Value;
            }
            var m3 = container[0x4489].FirstOrDefault() as EBMLType<decimal>;
            if (m3 != null)
            {
                Duration = m3.Value;
            }
        }
        protected override void InitDefaultValues()
        {
            TimecodeScale = 1000000;
            DateUTC = new DateTime(2000, 1, 1);
        }
        public decimal Duration { get; set; }
        public long TimecodeScale { get; set; }
        public DateTime DateUTC { get; set; }
        public string Title { get; set; }
    }
    public class Attachment : MatroskaBase
    {
        public Attachment(EBMLContainer container)
            : base(container)
        {

        }
        protected override void Initialize()
        {

        }
        protected override void InitDefaultValues()
        {
        }
    }
    public class EditionEntry : MatroskaBase
    {
        public EditionEntry(EBMLContainer container)
            : base(container)
        {

        }
        protected override void Initialize()
        {

        }
        protected override void InitDefaultValues()
        {
        }
    }
    public class SeekHead : MatroskaBase
    {
        public SeekHead(EBMLContainer container)
            : base(container)
        {

        }
        protected override void Initialize()
        {

        }
        protected override void InitDefaultValues()
        {
        }

        public IList<SeekInfo> Seeks { get; set; }
    }
    public class SeekInfo : MatroskaBase
    {
        public SeekInfo(EBMLContainer container)
            : base(container)
        {

        }
        protected override void Initialize()
        {

        }
        protected override void InitDefaultValues()
        {
        }

        public byte[] SeekID { get; set; }
        public long SeekPosition { get; set; }
    }
}
